/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Threads;

import DAO.TinTucDAO;
import DTO.TinTucDTO;
import GUI.pnNews;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author QuangHau
 */
public class loadTinTuc extends Thread {

    JScrollPane pnNews;
    JLabel lbstatus;
    int mawebsite;
    int machude;
    boolean write;

    public loadTinTuc(Object[] args) {
        pnNews = (JScrollPane) args[0];
        lbstatus = (JLabel) args[1];
        mawebsite = (int) args[2];
        machude = (int) args[3];
        write = (boolean) args[4];
    }

    @Override
    public void run() {
        if (write) {
            writeLbStatus wt = null;
            if (!Comvar.writeStatus) {
                Comvar.writeStatus = true;
                wt = new writeLbStatus(lbstatus, "Đang tải");
                wt.start();
            }
            loadTinTuc(mawebsite, machude);
            if (wt!= null) {
                wt.stopWrite();
            }

        } else {
            loadTinTuc(mawebsite, machude);
            Comvar.writeStatus = false;
        }
    }

    private void loadTinTuc(int maWebsite, int maChuDe) {
        JPanel pn = new JPanel();
        pn.setLayout(new BoxLayout(pn, BoxLayout.Y_AXIS));
        ArrayList<TinTucDTO> arr = TinTucDAO.SelectDanhSachTinTuc(maWebsite, maChuDe);
        if (arr != null & arr.size() > 0) {
            for (int i = 0; i < arr.size(); i++) {
                pn.add(new pnNews(arr.get(i), 1));
            }
            pnNews.getViewport().add(pn);
        } else {
            pnNews.getViewport().add(new JLabel("Không có tin tức"));
        }
    }
}
